<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 *
 *
 * @package     ceo
 * @subpackage  models
 * @category    models
 * @author      ydd team, wu
 * @copyright   copyright (c) 2012, yaoduoduo, Inc.
 * @filesource
 */
class Object_for_rent_model extends Base_model {
    protected $_table_name = 'object_for_rent';
    protected $_key_name = 'object_for_rent_id';
	
	function get_object_for_rent_info($id) {
		if(!empty($id))
			$where[$this->_key_name]=$id;
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'row');
	}

	function get_object_for_rent_name($name) {
		if(!empty($name))
			$where["name"]=$name;
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'row');
	}

	function get_object_for_rent_name_count($name) {
		if($name!="")
			$where["name"]=$name;
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'count');
	}

    function get_object_for_rent($uri_query,$limit) {
		$this->set_limit($limit);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where=array('dtime'=>0,'name'=>$uri_query['keyword']);
		return $this->fetch(NULL, $where);
    }
	function get_object_for_rent_count($uri_query) {
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$con["where"][] = array('dtime'=>0,'name'=>$uri_query['keyword']);
        $this->set_args_array($con);
        return $this->fetch_count();
	}
	function get_all() {
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where=array('dtime'=>0);
		return $this->fetch(NULL, $where);
    }
	
	
	function query_object_for_rent($w=null,$keyword=null,$limit) 
	{
		$this->set_limit($limit);
		if($keyword!="")
			$this->set_or_like('name',$keyword);
		if(isset($w['name']))
			$this->set_or_like('name',$w['name']);


		if(isset($w['sctime'])){
			$ts=explode("/",$w['sctime']);
			$where['ctime >='] =mktime(0,0,0,$ts[1],$ts[2],$ts[0]);
		}
		if(isset($w['ectime'])){
			$te=explode("/",$w['ectime']);
			$where['ctime <='] =mktime(23,59,59,$te[1],$te[2],$te[0]);
		}
		if(isset($w['broker']))
			$this->set_or_like('broker',$w['broker']);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime'] =0;
		return $this->fetch(NULL, $where);
    }
	function query_object_for_rent_count($w=null,$keyword=nulls)
	{
		if($keyword!="")
			$this->set_or_like('name',$keyword);
		if(isset($w['name']))
			$this->set_or_like('name',$w['name']);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		if(isset($w['sctime'])){
			$ts=explode("/",$w['sctime']);
			$where['ctime >='] =mktime(0,0,0,$ts[1],$ts[2],$ts[0]);
		}
		if(isset($w['ectime'])){
			$te=explode("/",$w['ectime']);
			$where['ctime <='] =mktime(23,59,59,$te[1],$te[2],$te[0]);
		}
		if(isset($w['broker']))
			$this->set_or_like('broker',$w['broker']);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime'] =0;
		return $this-> fetch(NULL, $where, NULL,"count");
	}
	
	function update_object_for_rent($data,$id) {
		$where[]=array('object_for_rent_id'=>$id);
		return $this->update($data, $where);
	}

	function add_object_for_rent($data) {		
		$data['shop_point_id']=$this->login_lib->shop_point_id();
		$data['ctime']=time();
		return $this->insert ( $data );
	}

	function del_object_for_rent($data) {
		$where[]=array('object_for_rent_id'=>$data['id']);
		$value=array('dtime'=>time());
		return $this->update($value, $where);
	}
	
}